var mongoose = require('mongoose');
var TeacherSchema = new mongoose.Schema({ //定义数据模型
    Id:String,//ID
    Name:String,//老师姓名
    Sex:{type:Number, default:-1},//性别
    Age:Number,//年龄
    CertificateType:String,//国家/学校名称
    Pro:String,//所在省
    City:String,//所在市
    Address:String,//详细地址
    Tel:String,//手机号/微信号
    Avatar:String,//头像
    Price:Number,//课时费(元/小时)
    TargetStudent:Number,//教学类型 (1:真人外交, 2:英语陪练)
    Introductions:String,//个人介绍
    Password:String,//密码
    status:{type:Number, default:0}//状态
});
mongoose.model('Teacher', TeacherSchema);//将该Schema发布为Model
var Teacher = mongoose.model('Teacher');//Teacher为model name


export function insertTeacher(options){
  //添加验证逻辑
  return new Promise((resolve, reject) => {
    if(!options.Id){
      reject("请填写教师Id");
      return;
    }
    if(!options.Name){
      reject("请填写教师姓名"); 
      return;
    }
    if(!options.Sex){
      reject("请填写教师性别");
      return;
    }
    if(!options.Age){
      reject("请填写教师年龄");
      return;
    }
    if(!options.CertificateType){
      reject("请填写国家/学校名称");
      return;
    }
    if(!options.Pro){
      reject("请填写教师所在省");
      return;
    }
    if(!options.City){
      reject("请填写教师所在市");
      return;
    }
    if(!options.Address){
      reject("请填写教师详细地址");
      return;
    }
    if(!options.Tel){
      reject("请填写手机号/微信号");
      return;
    }
    if(!options.Price){
      reject("请填写课时费");
      return;
    }
    if(!options.TargetStudent){
      reject("请填写教学类型");
      return;
    }
    if(!options.Password){
      reject("请填写密码");
      return;
    }
    

    var teacher = new Teacher({
			Id:options.Id,//ID
			Name:options.Name,//老师姓名
			Sex:options.Sex,//性别
      Age:options.Age,//年龄
      CertificateType:options.CertificateType,//机构/学校名称
			Pro:options.Pro,//所在省
			City:options.City,//所在市
			Address:options.Address,//详细地址
      Tel:options.Tel,//手机号/微信号
      Avatar:options.Avatar,//头像
      Price:options.Price,//课时费(元/小时)
      TargetStudent:options.TargetStudent,//教学类型 (1:真人外交, 2:英语陪练)
      Introductions:options.Introductions,//个人介绍
      Password:options.Password,//密码
		});
		
		teacher.save(function (err) {    //增加
			if(err){
				reject(err);
			}else{
				resolve("success");
			}
    });
    
  });
}

export function findTeacher(options,page){

  //添加验证逻辑
  return new Promise((resolve, reject) => {
    Teacher.find(options, "Id Name Avatar TargetStudent Price Sex Age").sort({Price:1}).skip(page*10).limit(10).exec(function (err,results) {  
			if(err){
				reject(err);
			}else{
				resolve(results);
			}
    })

  });
}

export function findOneTeacher(options){
  //添加验证逻辑
  return new Promise((resolve, reject) => {
    Teacher.findOne(options, "Id Name Sex Age CertificateType Pro City Address Tel Avatar Price TargetStudent Introductions", function (err,results) {  
			if(err){
				reject(err);
			}else{
				resolve(results);
			}
    });
  });
}

export function removeTeacher(options){
  //添加验证逻辑
  return new Promise((resolve, reject) => {
    if(!options.Id){
      reject("请传递教师Id");
      return;
    }
    Teacher.remove({Id:options.Id},function (err, result) { 
      if(!!err){
        reject(err);
      }else{
        resolve(result);
      }
    });
  });
}

export function removeAllTeacher(){
  //添加验证逻辑
  return new Promise((resolve, reject) => {
    Teacher.remove({},function (err, result) { 
      if(!!err){
        reject(err);
      }else{
        resolve(result);
      }
    });
  });
}

export function updateTeacher(options){
  delete options.status;
  
  return new Promise((resolve, reject) => {
    let ID = options._id;
    Teacher.update({_id: ID}, options, {}, function(err, docs){
      if(!!err){
        reject(err);
      }else{
        resolve("修改成功");
      }
    })
    
  });
}

